package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.dto.ShoppingCartDto;
import com.liusand.dto.SubShoppingCartDto;
import com.liusand.pojo.ShoppingCart;
import com.liusand.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :ShoppingCart
 * creationTime :2022/12/1 18:28
 */
@RequestMapping("shoppingCart")
@RestController
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("list")
    public Result list(){
        List<ShoppingCart> list = shoppingCartService.list();
        return Result.success(list);
    }

    @PostMapping("add")
    public Result joinShoppingCart(@RequestBody ShoppingCartDto shoppingCartDto){
        if(shoppingCartDto == null){
            return Result.error("加入购物车商品失败");
        }
        if(shoppingCartDto.getSetmealId() != null && shoppingCartDto.getDishId() != null){
            return Result.error("加入购物车商品失败");
        }
        if(shoppingCartDto.getSetmealId() == null && shoppingCartDto.getDishId() == null){
            return Result.error("加入购物车商品失败");
        }
        ShoppingCart joinResult = shoppingCartService.joinShoppingCart(shoppingCartDto);
        if(joinResult == null){
            return Result.error("加入购物车商品失败");
        }
        return Result.success(joinResult);
    }

    @PostMapping("sub")
    public Result subShoppingCart(@RequestBody SubShoppingCartDto subShoppingCartDto){
        if(subShoppingCartDto == null){
            return Result.error("删除购物车商品失败");
        }
        if(subShoppingCartDto.getSetmealId() != null && subShoppingCartDto.getDishId() != null){
            return Result.error("删除购物车商品失败");
        }
        if(subShoppingCartDto.getSetmealId() == null && subShoppingCartDto.getDishId() == null){
            return Result.error("删除购物车商品失败");
        }
        ShoppingCart subResult = shoppingCartService.subShoppingCart(subShoppingCartDto);
        return Result.success(subResult);
    }

    @DeleteMapping("clean")
    public Result cleanShoppingCart(){
        boolean cleanResult = shoppingCartService.cleanShoppingCart();
        if(!cleanResult){
            return Result.error("清空购物车商品失败");
        }
        return Result.success("清空购物车商品成功");
    }
}
